package com.wedu.dao.impl;

import com.wedu.dao.StudentDao;
import com.wedu.entity.Student;
import com.wedu.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.sql.SQLException;


public class StudentDaoImpl implements StudentDao {
    //获取JDBCTemplate对象
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    //根据姓名查找学生
    @Override
    public Student findOneById(int id) {
        //定义sql
        String sql = "select * from student where id = ?";
        Student student = template.queryForObject(sql, new BeanPropertyRowMapper<Student>(Student.class), id);
        return student;
    }

    //登录判断
    @Override
    public Student login(String name, String password) {
        //定义sql
        try {
            String sql = "select * from student where name = ? and password = ?";
            Student student = template.queryForObject(sql, new BeanPropertyRowMapper<Student>(Student.class), name, password);
            return student;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void addOne(Student student) {

    }

    @Override
    public void deleteStu(int id) {

    }

    //学生注册
    @Override
    public int register(String name, String password) {
        //定义sql
        try {
            String sql = "insert into student(`name`,password) values(?,?)";
            int update = template.update(sql, name, password);
            return update;
        } catch (Exception e) {
//            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public Student updateMsg(Student student) throws SQLException {
        Connection conn = JDBCUtils.getConnection();  //创建连接对象，获取数据库连接
        //定义sql
        try {
            conn.setAutoCommit(false);  //开启事务
            String sql = "UPDATE student SET  `name` = ? ,sex = ? , age = ? , `password` = ?  WHERE `id` = ? ";
            template.update(sql, student.getName(), student.getSex(), student.getAge(), student.getPassword(), student.getId());
            String sql2 = "select * from student where `id`= ?";
            Student stu2 = template.queryForObject(sql2, new BeanPropertyRowMapper<Student>(Student.class), student.getId());
            //若没有异常，则提交事务
            conn.commit();
            return stu2;
        } catch (Exception e) {
            e.printStackTrace();
            //若有异常，则回滚事务
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }return null;

        //        try {
//            String sql = "UPDATE * SET  `name` = ? ,sex = ? , age = ? , `password` = ?  WHERE `name` = ? and  `password` = ? ";
//            int update = template.update(sql,student);
//        }catch (Exception e) {
////            e.printStackTrace();
//            return null;
//        }
//        return null;

        //定义sql
//        try {
//            String sql = "UPDATE student SET  `name` = ? ,sex = ? , age = ? , `password` = ?  WHERE `name` = ? and `password` = ? ";
//            Student update = template.queryForObject(sql, new BeanPropertyRowMapper<Student>(Student.class),student);
//            return update;
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }

    }


}


